<?php

class Main_Model_Address extends WOW_Model_Abstract
{
    public function getResourceId()
    {
        return 'Address';
    }

    public function getAddressById($id)
    {
        $id = (int) $id;
        return $this->getResource('Address')->getAddressById($id);
    }
    
    public function getUserAddress($userid)
    {
        $userid = (int) $userid;
        $tem = $this->getResource('Address')->getUserAddress($userid);
        return $tem->toArray();
    }

    public function getUserCorrespondanceAddress($userid)
    {
        return $this->getResource('Address')->getUserCorrespondanceAddress($userid);
    }

    public function getUserEmergencyAddress($userid)
    {
       return $this->getResource('Address')->getUserEmergencyAddress($userid);
    }
    
    public function createAddress($post)
    {
        $data = array();
        if( isset( $post['address_type'] ) )
            $data['address_type'] = trim($post['address_type']);

        $data['uid'] = trim($post['uid']);
        
        if( isset( $post['hiddenAddressId']) )
            $data['hiddenAddressId'] = trim($post['hiddenAddressId']);
        
        if( isset( $post['AddressLine1']) )
            $data['AddressLine1'] = trim($post['AddressLine1']);

        if( isset( $post['AddressLine2']))
            $data['AddressLine2'] = trim($post['AddressLine2']);

         if( isset( $post['AddressCity']) )
            $data['AddressCity'] = trim($post['AddressCity']);

         if( isset( $post['AddressState']) )
            $data['AddressState'] = trim($post['AddressState']);
         
         if( isset( $post['AddressCountry']) )
            $data['AddressCountry'] = trim($post['AddressCountry']);
         
         if( isset( $post['AddressPIN']) )
            $data['AddressPIN'] = trim($post['AddressPIN']);
         
         if( isset( $post['AddressPhone1']) )
            $data['AddressPhone1'] = trim($post['AddressPhone1']);
          
         if( isset( $post['AddressPhone2']) )
            $data['AddressPhone2'] = trim($post['AddressPhone2']);
         
          if($this->_saveAddress($data) )
              return true;
          else
              return false;

    }
    
    protected function _saveAddress($data, $defaults=array()   )
    {
        foreach ($defaults as $col => $value)
        {
            $data[$col] = $value;
        }
        //$address = ($data['address_id'] !='') ? $this->getResource('Address')->getUserById($data['address_id']) : null;
        
        $address = ($data['hiddenAddressId'] !='') ? $this->getResource('Address')->getAddressById($data['hiddenAddressId']) : null;
        
        return $this->getResource('address')->saveRow($data, $address);
    }
}

?>
